package com.me.crgame.core.actores;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.crgame.core.ActorTouchListener;


public class ActorButtonAnimado extends ActorButton implements Animacion{
	
	
	private int        					FRAME_COLS;         
    private int        					FRAME_ROWS;         
    private float 						velocidadRefresco = 0.03f;
    private Animation                   animacion;          
    
    private Texture                     plantilla;              
    private TextureRegion[]             frames;                         
    private TextureRegion               frameActual;           
    
    private float stateTime;    
    
    
    private boolean playing = true;
    	
    
    public ActorButtonAnimado(String rutaAnimacion, ActorTouchListener touchListener, int filas, int cols) {
    	super();
		this.FRAME_ROWS = filas;
		this.FRAME_COLS = cols;
		
		this.touchListener = touchListener;
		this.addListener(new MiInputListener(this));
		
		isPressed = false;
		plantilla = new Texture(Gdx.files.internal(rutaAnimacion));
		this.textureRegion = trocear();
		this.setWidth(textureRegion.getRegionWidth());
		this.setHeight(textureRegion.getRegionHeight());
		
	}
    
    public ActorButtonAnimado(String rutaAnimacion, ActorTouchListener touchListener, int filas, int cols, float velocidadRefresco) {
    	super();
		this.FRAME_ROWS = filas;
		this.FRAME_COLS = cols;
		
		this.touchListener = touchListener;
		this.addListener(new MiInputListener(this));
		
		isPressed = false;
		plantilla = new Texture(Gdx.files.internal(rutaAnimacion));
		this.velocidadRefresco = velocidadRefresco;
		this.textureRegion = trocear();
		this.setWidth(textureRegion.getRegionWidth());
		this.setHeight(textureRegion.getRegionHeight());
		
	}
    
    public void cambiarVelocidadRefresco(float velocidadRefresco){
    	this.velocidadRefresco = velocidadRefresco;
    }
    
    
    private TextureRegion trocear(){
	     
		TextureRegion[][] tmp = TextureRegion.split(plantilla, plantilla.getWidth() / 
				FRAME_COLS, plantilla.getHeight() / FRAME_ROWS);                               
		frames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
		int index = 0;
		for (int i = 0; i < FRAME_ROWS; i++) {
			for (int j = 0; j < FRAME_COLS; j++) {
				frames[index++] = tmp[i][j];
			}
		}
		animacion = new Animation(velocidadRefresco, frames);                                          
		stateTime = 0f;                                  
		/*axis_x = 0;
		speed = 200;*/
		
		return animacion.getKeyFrame(stateTime, true);
	}
    
    @Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		// Esto llama a que el Actor refresque el estado de las acciones activas
		super.act(Gdx.graphics.getDeltaTime());

		colorActual = getColor();
		batch.setColor(colorActual.r, colorActual.g, colorActual.b,
				colorActual.a * parentAlpha);

		if(animacion!=null && playing){
			 stateTime += Gdx.graphics.getDeltaTime();                      
	         frameActual = animacion.getKeyFrame(stateTime, true);
		}
		
		batch.draw(frameActual, getX(), getY(), getOriginX(), getOriginY(),
				getWidth(), getHeight(), getScaleX(), getScaleY(),
				getRotation());

		/*if(font!=null && text!=null){

			float px = getX() - boundsOffsetX;
			float py = getY() + boundsOffsetY;

			font.setColor(Color.WHITE);
			font.draw(batch, text, px,py);
		}*/

	}

	@Override
	public void play() {
		playing = true;
	}

	@Override
	public void pause() {
		playing = false;
	}

	@Override
	public void pause(int secs) {
		playing = false;
		//TODO controlar los segundos, con otro hilo o temporizador
	}

	@Override
	public void stop() {
		playing = false;
		restart();
	}

	@Override
	public void restart() {
		stateTime = 0f; //Se vuelve al primer frame
	}

}
